package exceptionDemo;

public class Person {
	private String name;
	private int age;
	private String sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) throws AgeException {
		if (age > 0 && age < 100) {
			this.age = age;
			// 年龄赋值有问题，抛出异常
		} else {
			throw new AgeException("年龄必须在0到100之间");
		}
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) throws Exception {
		if (sex.equals("男") || sex.equals("女")) {
			this.sex = sex;
			// 性别赋值有问题，抛出异常
		} else {
			throw new Exception("性别必须是男或女");
		}
	}

	public static void main(String[] args) {
		Person person = new Person();
		try {
			person.setSex("公");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		try {
			person.setAge(122);
		} catch (AgeException ex1) {
			ex1.printStackTrace();
		}
	}
}
